<?php
 
class Gallery_MasterController extends Clu_Controller_Action_Master
{
	protected $m;
	public function init()
	{
		$this->m = new gallery_Model_Gallery();
		parent::init();
	}
	
	public function indexAction()
	{
		
	
	}
	public function uploadPhotosAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!$sess->userId)
		{
			$this->renderError(null, t(414));
			return;
		}
		$galleryId = $this->_request->getParam("id");
//		
//		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
//		$nService = new node_Model_NodeService();
//		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
//		$ret = $nService->getNodeData($nodeTypeInfo, $galleryId);
//		if ($ret['success'])
//			$gallery = $ret['node'];
//		else
//		{
//			$this->renderError(null, "gallery not found");
//			return;
//		}
		$this->paramsForWidgets["id"] = $galleryId;
		$this->paramsForWidgets["mode"] = "upload";
//		$this->viewParams["gallery"] = $gallery;
//		die_a($gallery);
	}
	
	/*
	 * Copied from 'file' module
	 */
	public function postImage($galleryId, $userId, $createWM)
	{
		//require_once APP_PATH."/library/common.php";
		$watermarkDir = APP_PATH."/public/watermark/";

		//see generate_image_copies() @ /library/common.php
		$ustoreDir = APP_PATH."/public/ustore/$userId/images/original/$contentType/";
		if (!file_exists($ustoreDir))
			mkdir($ustoreDir, 0777  , true);

		$ufilesDir = APP_PATH."/public/ufiles/$userId/images/original/$contentType/" ; //just for avatar
		if (!file_exists($ufilesDir))
			mkdir($ufilesDir, 0777  , true);
		$id = $this->_request->getParam('id');
		$adapter= new Zend_File_Transfer_Adapter_Http();

		//normal content type like place#2135 is different from uploading avatar
		if ($contentType == 'avatar')
    	{
    		$originalFilename = "avatar.jpg";
    		$configSizes = array("xxsmall", "xsmall", "small", "medium");
    		$configSizesInPixel = array("30", "75", "100", "260");
    		//delete current images first
    		foreach ($configSizes as $size)
    		{
    			unlink(APP_PATH."/public/ufiles/$userId/images/$size/avatar/avatar.jpg");
    		}
    	}
		else //uploading content is a little different from avatar as there is only 1 avatar per user
		{
    		$extension_path = pathinfo($_FILES['file_upload']['name']);
    		$fileName = $extension_path['filename'];
    		$extension =  $extension_path['extension'];
    		
    		if (empty($extension))
    		{		
    			$mimeType = $_FILES['file_upload']['type'];
    			$extension = explode('/', $mimeType);
    		}
    
    		$saneFileName = SanitizeFileName($fileName);
    		$i = 1;
    		while(file_exists($ustoreDir. $contentId . '_' . $saneFileName . "." . $extension))
    		{
    			$saneFileName = $saneFileName . "_" .$i;
    			$i++;
    		}
    		$originalFilename =  $contentId . '_' . $saneFileName . "." . $extension;
    		//$originalFilename = $contentId . '_' . $saneFileName . "." . $extension
		}
		//now that we have the file name. Let's get it    		    	          	
		try 
		{
			$adapter->addFilter('Rename', array('target' => $ustoreDir. $originalFilename, 'overwrite' => true));
			$adapter->receive();
		}
		catch (Zend_File_Transfer_Exception $e) 
		{
		     $e->getMessage();
		}
		chmod($ustoreDir. $originalFilename, 0666);
		//die_a(array("ustore", $ustoreDir, $saneFileName, $watermarkDir, null, null, $userId, true) );
		if ($contentType == 'avatar')
    		generate_image_copies ("ustore", $ustoreDir, $originalFilename, $watermarkDir, $configSizes, $configSizesInPixel, $userId, false);    	
		else 
		{
			generate_image_copies ("ustore", $ustoreDir,$originalFilename, $watermarkDir, null, null, $userId, false); //with wm
			//die_a(array("ustore", $ustoreDir,$originalFilename, $watermarkDir, null, null, $userId, true));
		}                
	}
	
	/*
	 * Copy paste from node/master/view because gallery should be treated differently
 	 */
	
		
	public function viewAction()
	{
		$nodeType = 'gallery';
		$sess = new Zend_Session_Namespace(CLU_NS);
		//require_once (APP_PATH . "/modules/node/Model/NodeService.php");
		$this->mService = new node_Model_NodeService();
		
		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type does not exist");
		}
		//new view the node
		$nodeId = $this->_request->getParam('id');
		$ret = $this->mService->getNodeData($nodeTypeInfo, $nodeId, array('get_article' => 1));
		if ($ret['success'])
		{
			$this->viewParams['node'] = $ret['node'];
			if ($nodeTypeInfo['needs_translation'] && $sess->viewLang != get_language_from_int($ret['node']['language']))
				$this->viewParams['article'] = $ret['article'];
			$this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
			$this->mService->updateDbAfterView($nodeTypeInfo, $nodeId);
		}
		$this->paramsForWidgets["id"] = $nodeId;
	}
	
}
?>
